package com.scapegoat.pay.point.service.api.impl;

import com.scapegoat.pay.point.service.api.RpPointAccountQueryService;
import com.scapegoat.pay.point.service.dao.RpPointAccountDao;
import com.scapegoat.pay.point.service.entity.RpPointAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scapegoat.pay.common.core.enums.PublicStatusEnum;
import com.scapegoat.pay.common.core.page.PageBean;
import com.scapegoat.pay.common.core.page.PageParam;
import com.scapegoat.pay.common.core.utils.DateUtils;
import com.scapegoat.pay.point.service.exceptions.PointBizException;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账户查询service实现类
 *
 * @author walkman
 * @date 2017年10月17日 22:01:22
 */
@Service("rpPointAccountQueryService")
public class RpPointAccountQueryServiceImpl implements RpPointAccountQueryService {

    private static final Logger LOG = LoggerFactory.getLogger(RpPointAccountQueryServiceImpl.class);

    @Autowired
    private RpPointAccountDao rpPointAccountDao;

    /**
     * 根据用户编号编号获取账户信息
     *
     * @param userNo 用户编号
     * @return
     */
    public RpPointAccount getAccountByUserNo(String userNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userNo", userNo);
        LOG.info("根据用户编号查询账户信息");
        RpPointAccount account = this.rpPointAccountDao.getBy(map);
        if (account == null) {
            throw PointBizException.ACCOUNT_NOT_EXIT;
        }
        // 不是同一天直接清0
        if (!DateUtils.isSameDayWithToday(account.getEditTime())) {
            account.setEditTime(new Date());
            rpPointAccountDao.update(account);
        }
        return account;
    }


    /**
     * 根据参数分页查询账户.
     *
     * @param pageParam 分页参数.
     * @param params    查询参数，可以为null.
     * @return AccountList.
     * @throws
     */
    public PageBean queryAccountListPage(PageParam pageParam, Map<String, Object> params) {

        return rpPointAccountDao.listPage(pageParam, params);
    }

    /**
     * 获取所有账户
     *
     * @return
     */
    @Override
    public List<RpPointAccount> listAll() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("status", PublicStatusEnum.ACTIVE.name());
        return rpPointAccountDao.listBy(paramMap);
    }

}